package hkr.vfu.bluecenter.activities;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;

import hkr.vfu.bluecenter.R;
import hkr.vfu.bluecenter.backend.DataSingleton;
import hkr.vfu.bluecenter.backend.SensorVO;


/**
 * Created by MathiasHansson on 2013-12-13.
 */
public class SensorSettingsActivity extends FragmentActivity implements AdapterView.OnItemSelectedListener, Button.OnClickListener {

    private Spinner spinner;
    private DataSingleton dataSingleton;
    private EditText kEditText;
    private EditText mEditText;
    private EditText sensorNameEditText;
    private SensorVO selectedSensor;
    private EditText endString;
    private String MAC;

    @Override
    public void onCreate(Bundle savedInstaceState) {
        super.onCreate(savedInstaceState);
        setContentView(R.layout.digital_analog);
        dataSingleton = DataSingleton.getInstance();
        MAC = getIntent().getStringExtra("MAC");

        SensorVO[] connectedSensors = dataSingleton.getBoxWithMAC(MAC).getSensors();

        selectedSensor = connectedSensors[getIntent().getIntExtra("position", 0)];

        mEditText = (EditText) findViewById(R.id.setM_editText);
        kEditText = (EditText) findViewById(R.id.coefficient_editText);
        endString = (EditText) findViewById(R.id.end_string_editText);

        sensorNameEditText = (EditText) findViewById(R.id.sensor_name_editText);

        String k = "" + selectedSensor.getK();
        kEditText.setText(k);

        sensorNameEditText.setText(selectedSensor.getSensorName());
        endString.setText(selectedSensor.getUnitString());

        spinner = (Spinner) findViewById(R.id.sensor_choice_spinner);
        ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.sensor_array, android.R.layout.simple_spinner_dropdown_item);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinnerArrayAdapter);
        spinner.setSelection(selectedSensor.getType());
        spinner.setOnItemSelectedListener(this);

        ImageButton saveChangesButton = (ImageButton) findViewById(R.id.save_sensor_setting_changes_button);
        saveChangesButton.setOnClickListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String[] spinnerList = getResources().getStringArray(R.array.sensor_array);
        if (spinnerList[i].equals("Analogous")) {
            mEditText.setEnabled(true);
            String m = "" + selectedSensor.getM();
            mEditText.setText(m);
        } else {
            mEditText.setEnabled(false);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    /**
     * Called when the save button is clicked
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        selectedSensor.setK(Double.parseDouble(kEditText.getText().toString()));
        if (mEditText.isEnabled()) {
            selectedSensor.setM(Double.parseDouble(mEditText.getText().toString()));
        } else {
            selectedSensor.setM(0);
        }
        selectedSensor.setSensorName(sensorNameEditText.getText().toString());
        selectedSensor.setType(spinner.getSelectedItemPosition());
        selectedSensor.setUnitString(endString.getText().toString());

        finish();
    }
}
